//
//  EBMBaseMessageViewController.m
//  eBondMall
//
//  Created by apple on 2020/3/31.
//  Copyright © 2020 zhaoshijie. All rights reserved.
//

#import "EBMBaseMessageViewController.h"

@interface EBMBaseMessageViewController ()

@end

@implementation EBMBaseMessageViewController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    

    //  设置IM未读消息数
    [self settingIMChatCount];
    
    
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //  初始化视图
    [self baseMessageInitView];
    
}




#pragma mark ----------初始化视图----------


//  初始化视图
- (void)baseMessageInitView
{
    //  创建控件    消息按钮
//    [self baseMessageCreateViewInMessage];
}




/// 创建控件    右侧按钮
/// @param title 标题
/// @param imageName 图片名称
/// @param buttonClickBlock 回调
- (void)baseMessageCreateViewInNavigationBarRightButtonWtihTitle:(NSString *)title
                                                       imageName:(NSString *)imageName
                                                buttonClickBlock:(OperationBlock)buttonClickBlock
{
    
    [super baseCreateViewInNavigationBarRightButtonWtihTitle:title imageName:imageName buttonClickBlock:buttonClickBlock];
    
//    [self.nvigationRightBarButtonItemsMArray insertObject:self.messageItem atIndex:0];
    
    self.navigationItem.rightBarButtonItems = self.nvigationRightBarButtonItemsMArray;
    
}



//  创建控件    消息按钮
- (void)baseMessageCreateViewInMessage
{
    
    UIButton *messageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    messageButton.frame = CGRectMake(0, 0, 34, 44);
    [messageButton setImage:[UIImage imageNamed:@"Navigation_Message"] forState:UIControlStateNormal];
    [messageButton addTarget:self action:@selector(messageButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.noReadMessageLabel = [[UILabel alloc]initWithFrame:CGRectMake(44-8-16, 5, 16, 16)];
    self.noReadMessageLabel.backgroundColor = RGBCOLOR16_EB5D5D;
    self.noReadMessageLabel.layer.cornerRadius = 16/2.0;
    self.noReadMessageLabel.layer.masksToBounds = YES;
    self.noReadMessageLabel.font = Font_10;
    self.noReadMessageLabel.textAlignment = NSTextAlignmentCenter;
    self.noReadMessageLabel.textColor = [UIColor whiteColor];
    self.noReadMessageLabel.text = @"99+";
//    self.noReadMessageLabel.adjustsFontSizeToFitWidth = YES;
    [messageButton addSubview:self.noReadMessageLabel];
    self.noReadMessageLabel.hidden = YES;
    
    self.messageItem = [[UIBarButtonItem alloc]initWithCustomView:messageButton];
    
    self.navigationItem.rightBarButtonItem = self.messageItem;
}


#pragma mark --------------方法--------------


//  设置未读信息
- (void)settingNoReadMessageWithLabel:(UILabel *)label
                               number:(NSInteger)number
{
    
    NSString *count = @(number).stringValue;
    
    if(number>0 && number<100)
    {
        label.hidden = NO;
    }
    else if (number>=100)
    {
        label.hidden = NO;
        count = @"99+";
    }
    else
    {
        label.hidden = YES;
    }
    
    float width = [count stringComputeSizeWithFont:Font_10 boundWidth:100 boundHeight:12].width;
    
    if(width>11.5)
    {
        label.width = width+3*2;
    }
    else
    {
        label.width = 16;
    }
    
    label.text = count;
    
}



//  设置IM未读消息数
- (void)settingIMChatCount
{
    
    
    if(KUserSingeIsLogin)
    {
        
    }
    else
    {
        [self settingNoReadMessageWithLabel:self.noReadMessageLabel number:0];
    }
    
    
}


#pragma mark --------------响应方法--------------

//  消息点击
- (void)messageButtonClick:(UIButton *)sender
{
    if(self.navigationRightMessageButtonBlock)
    {
        self.navigationRightMessageButtonBlock();
    }
    
}


@end
